import { AxiosResponse } from "axios"
import { SortInfoType, VmsPageTableColumn, VmsPageTableColumnFilterType } from "./interface"
import { commonList } from "/@/api/org/code-list"

/**
 * 排序顺序转换
 * @param sortOrder 
 */
export function sortOrderTransform(sortOrder: string): string {
    if (sortOrder == "ascending") return 'asc'
    if (sortOrder == "descending") return 'desc'
    return "asc"
}

/**
 * 处理查询参数
 * @param pagination 分页参数
 * @param filterInfo 过滤参数
 * @param sortInfo 排序
 * @param config 配置
 */
export function queryParamHandler(pagination: any, filterInfo: any, sortInfo: SortInfoType, config: any): any {
    //处理分页
    const queryparam: any = { page: pagination.currentPage, rows: pagination.currentPageSize }

    //处理筛选
    if (filterInfo) {
        for (var key in filterInfo) {
            if (filterInfo[key].length > 0) {
                queryparam[key] = filterInfo[key].join(",")
            } else {
                delete queryparam[key]
            }
        }
    }
    //处理排序
    if (sortInfo && sortInfo.direction && sortInfo.property) {
        queryparam.sort = sortInfo.property + " " + sortInfo.direction
    }
    return queryparam
}

/**
 * 表格头部处理
 * @param tableColumn 表格头部配置数据
 * @param filterInfo 存放过滤参数的值
 * @param sortMappingMap 存放排序参数的值
 */
export function tableColumnHandler(tableColumn: Array<VmsPageTableColumn>, filterInfo: any, sortMappingMap: Map<string, string>) {
    tableColumn.forEach(column => {
        tableColumnFilterHandler(column, filterInfo)
        tableColumnSortHandler(column, sortMappingMap)
    })
}

/**
 * 处理表格头部的filter
 * @param tableColumn 表格头部配置数据
 * @param filterInfo 存放过滤参数的值
 */
function tableColumnFilterHandler(tableColumn: VmsPageTableColumn, filterInfo: any) {
    if (tableColumn.filter) {
        //处理filters内容
        if (tableColumn.filter.type === VmsPageTableColumnFilterType.DICTIOANRY) {
            tableColumn.filter.filters = []//这里给个默认值 不然数组变化不会被监听
            //todo 获取数据字典
            if (tableColumn.filter.codeItemId) {
                commonList(tableColumn.filter.codeItemId).then(res => {
                    let _arr: any[] = []
                    res.data.forEach(item => {
                        _arr.push({ text: item.value, value: item.key })
                    })
                    if (tableColumn.filter) tableColumn.filter.filters = _arr
                })
            } else {
                console.warn("请配置codeItemId选项", tableColumn)
            }
        } else if (tableColumn.filter.type === VmsPageTableColumnFilterType.FUNCTION) {
            tableColumn.filter.filters = []//这里给个默认值 不然数组变化不会被监听
            if (tableColumn.filter.loadData) {
                tableColumn.filter.loadData().then((res: AxiosResponse<any[]>) => {
                    let _arr: any[] = []
                    let textKey = tableColumn.filter?.textKey || 'text'
                    let valueKey = tableColumn.filter?.valueKey || 'value'
                    res.data.forEach(item => {
                        _arr.push({ text: item[textKey], value: item[valueKey] })
                    })
                    if (tableColumn.filter) tableColumn.filter.filters = _arr
                })
                tableColumn.filter.filters = []
            } else {
                console.warn("请配置loadData函数")
            }
            //调用自己的自定义方法
        } else if (tableColumn.filter.type === VmsPageTableColumnFilterType.STATIC) {
            if (tableColumn.filter.filters) {
                tableColumn.filter.filters = [...tableColumn.filter.filters]
            }
        }
        //处理默认选中的
        if (tableColumn.filter.filteredValue && tableColumn.columnKey) {
            if (!filterInfo) filterInfo = {}
            filterInfo[tableColumn.columnKey] = tableColumn.filter.filteredValue
        }
    }
}

/**
 * 处理表格头部的排序 porp 与 customOrderBy映射
 * @param tableColumn 表格头部配置数据
 * @param sortMappingMap 存放排序参数的值
 */
function tableColumnSortHandler(tableColumn: VmsPageTableColumn, sortMappingMap: Map<string, string>) {
    if (tableColumn.sort && tableColumn.sort.customOrderby) {
        if (!sortMappingMap) sortMappingMap = new Map()
        if (tableColumn.prop) sortMappingMap.set(tableColumn.prop, tableColumn.sort.customOrderby)
    }
}

/**
 * @description: 
 * @param fileName 文件名
 * @param blob 下载的对象
 * @return: 
 * @author: Zhu_liangyu
 */
export function downloadFile(fileName: string, blob: Blob) {
    const elink = document.createElement("a")
    elink.download = fileName
    elink.style.display = "none"
    let binaryData = []
    binaryData.push(blob)
    elink.href = window.URL.createObjectURL(new Blob(binaryData))
    document.body.appendChild(elink)
    elink.click()
    URL.revokeObjectURL(elink.href) // 释放URL 对象
    document.body.removeChild(elink)
}


// 表格的高度适配
// 需要在mounted后调用
// tableRef:表格实例 /tableParentRef：表格父节点的实例 /tableFooterRef 如果有底部 底部实例 fit：高度适配方式 / tableHeight 表格高度
export function tableHeightFit(tableRef: any, tableParentRef: any, tableFooterRef: any | undefined, fit = 'auto', tableHeight: any,) {
    var getTableHeight = () => {
        let clientY = document.body.clientHeight
        if (tableParentRef) {
            let tableHeight = clientY - tableParentRef.getBoundingClientRect().top - 12
            tableParentRef.style.height = tableHeight + 'px'
        }
    }
    if (tableParentRef && fit === "full") {
        setInterval(getTableHeight, 10)
    } else if (fit === "auto") {
        if (tableHeight === "auto") {
            if (tableFooterRef) tableFooterRef.style.position = "static"
        } else {
            tableParentRef.style.height = typeof tableHeight === "number" ? tableHeight + "px" : tableHeight
        }
    }
}
